﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.model;
using Peekaboo2008.dao;

namespace Peekaboo2008.view
{
    public partial class FormChiTietSanPhamHangHoa : Form
    {
        private FormSanPhamHangHoa formSanPham;
        private HangHoaModel hangHoaModel = null;

        public FormChiTietSanPhamHangHoa(ref FormSanPhamHangHoa _formSanPham)
        {
            InitializeComponent();
            FormChiTietSanPhamHangHoaLoad();
            formSanPham = _formSanPham;
            initDonViQuiDoi();
        }

        private void initDonViQuiDoi(){
            cbbDonViQuiDoi.DataSource = DonViQuiDoiDAO.GetAll();
            cbbDonViQuiDoi.DisplayMember = "Don_vi_goc";
            cbbDonViQuiDoi.ValueMember = "ID";
        }
        public FormChiTietSanPhamHangHoa(HangHoaModel _hangHoaModel, ref FormSanPhamHangHoa _formSanPham)
        {
            InitializeComponent();
            FormChiTietSanPhamHangHoaLoad();
            formSanPham = _formSanPham;
            initDonViQuiDoi();
            // load chi tiet hang hoa
            hangHoaModel = _hangHoaModel;
            txtChietKhau.Text = hangHoaModel.ChietKhau;
            txtThue.Text = hangHoaModel.Thue;
            txtTenHang.Text = hangHoaModel.Ten;
            txtMaHang.Text = hangHoaModel.MaHang;
            rtbGhiChu.Text = hangHoaModel.GhiChu;
            rtbMoTa.Text = hangHoaModel.MoTa;
            txtMaVach.Text = hangHoaModel.BarCodeNew;
            txtGiaBanLe.Text = hangHoaModel.GiaBanLe;
            txtGiaBanBuon.Text = hangHoaModel.GiaBanSi;
            txtGiaNhap.Text = hangHoaModel.GiaNhap;
            cbbDonViQuiDoi.SelectedValue = _hangHoaModel.DonVi;
            txtDinhMucTon.Text = hangHoaModel.TonDinhMuc;
            txtMaVach.Text = hangHoaModel.BarCodeNew;
            cbbNhomHang.SelectedValue = hangHoaModel.MaNhomHang;
        }

        public void FormChiTietSanPhamHangHoaLoad() 
        {
            DataTable dtNhomHang = NhomHangDAO.getAllOrderByName();
            cbbNhomHang.DataSource = dtNhomHang;
            cbbNhomHang.ValueMember = "ID";
            cbbNhomHang.DisplayMember = "Ten";
        }

        private void FormChiTietSanPhamHangHoa_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormChiTietSanPhamHangHoa_KeyDown);
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (txtTenHang.Text.Equals(""))
            {
                MessageBox.Show("Tên Hàng Không Được Rỗng");
                txtTenHang.Focus();
            }
            else if (txtMaHang.Text.Equals(""))
            {
                MessageBox.Show("Mã Hàng Không Được Rỗng");
                txtMaHang.Focus();
            }
            else if (txtGiaNhap.Text.Equals(""))
            {
                MessageBox.Show("Giá Nhập Không Được Rỗng");
                txtGiaNhap.Focus();
            }
            else if (txtGiaBanBuon.Text.Equals(""))
            {
                MessageBox.Show("Giá Sĩ Không Được Rỗng");
                txtGiaBanBuon.Focus();
            }
            else if (txtGiaBanLe.Text.Equals(""))
            {
                MessageBox.Show("Giá Lẻ Không Được Rỗng");
                txtGiaBanLe.Focus();
            }
            else
            {
                if (hangHoaModel == null)
                {
                    hangHoaModel = new HangHoaModel();
                }

                hangHoaModel.Ten = txtTenHang.Text;
                hangHoaModel.MaNhomHang = cbbNhomHang.SelectedValue.ToString();
                hangHoaModel.MaHang = txtMaHang.Text;
                hangHoaModel.BarCodeNew = txtMaVach.Text;
                hangHoaModel.MoTa = rtbMoTa.Text;
                hangHoaModel.GiaNhap = txtGiaNhap.Text;
                hangHoaModel.GiaBanSi = txtGiaBanBuon.Text;
                hangHoaModel.GiaBanLe = txtGiaBanLe.Text;
                hangHoaModel.Thue = txtThue.Text;
                hangHoaModel.ChietKhau = txtChietKhau.Text;
                hangHoaModel.Hinh = "";
                hangHoaModel.DonVi = cbbDonViQuiDoi.SelectedValue.ToString();
                hangHoaModel.TonDinhMuc = txtDinhMucTon.Text;
                hangHoaModel.BarCodeNew = txtMaVach.Text.Trim();
                hangHoaModel.GhiChu = rtbGhiChu.Text;

                //Insert new
                if (hangHoaModel.ID == null || hangHoaModel.ID == "")
                {
                    if (HangHoaDAO.InsertHangHoa(hangHoaModel))
                    {
                        MessageBox.Show("Thêm Mới Hàng Hoá Thành Công");
                        formSanPham.FormSanPhamHangHoaLoad("","");
                        this.Dispose();
                    }
                }
                else
                {
                    if (HangHoaDAO.UpdateHangHoa(hangHoaModel))
                    {
                        MessageBox.Show("Cập Nhật Hàng Hoá Thành Công");
                        formSanPham.FormSanPhamHangHoaLoad("","");
                        this.Dispose();
                    }
                }
            }
        }

        private void FormChiTietSanPhamHangHoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F6)
            {
                btThemKhuVuc.PerformClick();
            }
            if (e.KeyCode == Keys.F8)
            {
                btnChapNhan.PerformClick();
            }
            if (e.KeyCode == Keys.F12)
            {
                this.Dispose();
            }
        }

        private void btThemKhuVuc_Click(object sender, EventArgs e)
        {
            FormChiTietSanPhamHangHoa formChiTietSanPhamHH = this;
            FormChiTietNhomHangHoa formChiTietNhomHangHoa = new FormChiTietNhomHangHoa(ref formChiTietSanPhamHH);
            formChiTietNhomHangHoa.ShowDialog();
        }

        private void txtGiaNhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaBanBuon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaBanLe_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtDinhMucTon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnKetThuc_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        
        

    }
}
